package com.github.tetrisgame.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewbinding.ViewBinding;

import com.github.tetrisgame.R;
import com.github.tetrisgame.base.BaseActivity;
import com.github.tetrisgame.databinding.ActivityVerifyBinding;

import cn.bmob.v3.BmobSMS;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class VerifyActivity extends BaseActivity<ActivityVerifyBinding> {

    @Override
    public ActivityVerifyBinding initBinding() {
        return ActivityVerifyBinding.inflate(getLayoutInflater());
    }

    @Override
    public void initUI() {
        //获取上一个Activity传递过来的Intent意图对象
        Intent intent = getIntent();
        //获取意图对象中存储的数据 key-value
        //TODO 测试完毕之后需要使用上一个界面传递的手机号
        //String phone = intent.getStringExtra("phone");
        String phone = "18502332377";

        //TextView tv = findViewById(R.id.tvPhone);
        mBinding.tvPhone.setText( phone );

        //监听验证码输入的回调事件
        mBinding.inputView.setOnInputListener(code ->{
            //去bmob中验证验证码是否正确
            BmobSMS.verifySmsCode(phone, code, new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e == null){
                        Intent intent = new Intent(VerifyActivity.this, MainActivity.class);
                        startActivity(intent);
                    }else{
                        Toast.makeText(VerifyActivity.this,"验证码不正确",Toast.LENGTH_SHORT).show();
                        mBinding.inputView.postDelayed(()->{
                            mBinding.inputView.clear();
                        },1000);
                    }
                }
            });
        });
    }

}